package com.techsoul.module.order.controller;

import com.techsoul.common.core.pojo.Result;
import com.techsoul.common.log.annotation.Log;
import com.techsoul.common.log.enums.OperationTypeEnum;
import com.techsoul.module.order.pojo.param.UserAddressSaveParam;
import com.techsoul.module.order.pojo.param.UserAddressSearchParam;
import com.techsoul.module.order.service.IUserAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * 用户地址 Controller
 *
 * @author liuen
 * @date 2021-04-06
 */
@Api(tags = "用户地址")
@RestController
@RequestMapping("/order/userAddress")
public class UserAddressController {

    @Autowired
    private Result result;
    
    @Autowired
    private IUserAddressService service;

    @ApiOperation("新增")
//    @PreAuthorize("hasAnyAuthority('order:userAddress:insert')")
    @Log(title = "用户地址", operationType = OperationTypeEnum.INSERT)
    @PostMapping
    public Result insert(@Valid @RequestBody UserAddressSaveParam params) {
        return result.success(this.service.insert(params));
    }

    @ApiOperation("批量删除（多个用逗号隔开）")
   // @PreAuthorize("hasAnyAuthority('order:userAddress:delete')")
    @Log(title = "用户地址", operationType = OperationTypeEnum.DELETE)
    @DeleteMapping("/{ids}")
    public Result batchDelete(@PathVariable String[] ids) {
        return result.success(this.service.batchDelete(Arrays.asList(ids)));
    }

    @ApiOperation("修改")
//    @PreAuthorize("hasAnyAuthority('order:userAddress:update')")
    @Log(title = "用户地址", operationType = OperationTypeEnum.UPDATE)
    @PutMapping
    public Result update(@Valid @RequestBody UserAddressSaveParam params) {
        return result.success(this.service.update(params));
    }

    @ApiOperation("更新用户默认地址")
//    @PreAuthorize("hasAnyAuthority('order:userAddress:update')")
    @Log(title = "用户地址", operationType = OperationTypeEnum.UPDATE)
    @PutMapping("/updateDefaultAddress")
    public Result updateDefaultAddress(@Valid @RequestBody UserAddressSaveParam params) {
        this.service.updateDefaultAddress(params);
        return result.success();
    }

    @ApiOperation("详情")
//    @PreAuthorize("hasAnyAuthority('order:userAddress:search')")
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") String id) {
        return result.success(this.service.findById(id));
    }

    @ApiOperation("列表")
   // @PreAuthorize("hasAnyAuthority('order:userAddress:search')")
    @PostMapping("/search")
    public Result findAll(@RequestBody UserAddressSearchParam params) {
        return result.success(this.service.findAll(params));
    }

    @ApiOperation("列表-分页")
//    @PreAuthorize("hasAnyAuthority('order:userAddress:search')")
    @PostMapping("/search/{current}/{pageSize}")
    public Result findAll(@RequestBody UserAddressSearchParam params, @PathVariable Integer current, @PathVariable Integer pageSize) {
        return result.success(this.service.findAll(params, current, pageSize));
    }

    @ApiOperation("用户收货地址")
    //@PreAuthorize("hasAnyAuthority('order:userAddress:search')")
    @PostMapping("/search/myAddressList")
    public Result findMyAddressList() {
        return result.success(this.service.findMyAddressList());
    }

    @ApiOperation("获取用户默认收货地址")
   //@PreAuthorize("hasAnyAuthority('order:userAddress:search')")
    @GetMapping("/search/defaultAddress")
    public Result findDefaultAddress() {
        return result.success(this.service.findDefaultAddress());
    }

}
